$().ready(function() {
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {

	$("#examIntro").val($('#content_sn').summernote('code'));
	$("#trainingExplain").val($('#trainingForm_sn').summernote('code'));
	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/examRegistration/saveExamRegistration",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}
		}
	});

}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入姓名"
			}
		}
	})
}

layui.use('laydate', function(){
	var laydate = layui.laydate;

	laydate.render({
		elem: '#times'
		//设置开始日期、日期日期的 input 选择器
		//数组格式为 2.6.6 开始新增，之前版本直接配置 true 或任意分割字符即可
		,range: ['#registrationStartDate', '#registrationEndDate']
	});

	laydate.render({
		elem: '#timesOut'
		//设置开始日期、日期日期的 input 选择器
		//数组格式为 2.6.6 开始新增，之前版本直接配置 true 或任意分割字符即可
		,range: ['#trainingStartDate', '#trainingEndDate']
	});

	laydate.render({
		elem: '#classMeetingDate'
		,type: 'datetime'
	});

	laydate.render({
		elem: '#graduationDate'
		,type: 'datetime'
	});

})

$().ready(function() {

	$("#content_sn").html($("#examIntro").val())
	$("#trainingForm_sn").html($("#trainingExplain").val())

	$('#trainingForm_sn').summernote({
		height: '300px',
		lang: 'zh-CN',
		toolbar: [
			['style', ['bold', 'italic', 'underline', 'clear']],
			['font', ['strikethrough', 'superscript', 'subscript']],
			['fontsize', ['fontsize']],
			['fontname', ['fontname']],
			['color', ['color']],
			['para', ['ul', 'ol', 'paragraph']],
			['height', ['height']],
			['table', ['table']],
			['insert', ['link', 'picture']],
			['view', ['fullscreen', 'codeview', 'help']]
		],
		callbacks: {
			onImageUpload: function (files, editor, $editable) {
				sendFiles(files,'#trainingForm_sn');
			},
			onPaste: function (ne) {
				var bufferText = ((ne.originalEvent || ne).clipboardData || window.clipboardData).getData('Text/plain');
				ne.preventDefault ? ne.preventDefault() : (ne.returnValue = false);
				setTimeout(function () {
					document.execCommand("insertText", false, bufferText);
				}, 10);
			}
		}

	});

	$('#content_sn').summernote({
		height: '300px',
		lang: 'zh-CN',
		toolbar: [
			['style', ['bold', 'italic', 'underline', 'clear']],
			['font', ['strikethrough', 'superscript', 'subscript']],
			['fontsize', ['fontsize']],
			['fontname', ['fontname']],
			['color', ['color']],
			['para', ['ul', 'ol', 'paragraph']],
			['height', ['height']],
			['table', ['table']],
			['insert', ['link', 'picture']],
			['view', ['fullscreen', 'codeview', 'help']]
		],
		callbacks: {
			onImageUpload: function (files, editor, $editable) {
				sendFiles(files,'#content_sn');
			},
			onPaste: function (ne) {
				var bufferText = ((ne.originalEvent || ne).clipboardData || window.clipboardData).getData('Text/plain');
				ne.preventDefault ? ne.preventDefault() : (ne.returnValue = false);
				setTimeout(function () {
					document.execCommand("insertText", false, bufferText);
				}, 10);
			}
		}
	});

})

//编辑器新增的ajax上传图片函数
function sendFiles(files, editor, $editable) {
	var formData = new FormData();
	formData.append("file", files[0]);
	$.ajax({
		data : formData,
		type : "POST",
		url : "/common/sysFile/upload",    // 图片上传出来的url，返回的是图片上传后的路径，http格式
		cache : false,
		contentType : false,
		processData : false,
		dataType : "json",
		success: function(data) {//data是返回的hash,key之类的值，key是定义的文件名
			$(editor).summernote('insertImage',data.data);
		},
		error:function(data){
			//console.log("data",data)
			alert("上传失败");
		}
	});
}

layui.use('upload', function () {
	var upload = layui.upload;
	upload.render({
		elem: '#btnAdd',
		url: '/api/oss/upload1',
		accept: 'pdf', //只允许上传图片
		acceptMime: 'pdf/*', //只筛选图片
		exts:'pdf',
		size:10240,
		multiple:true,
		number:5,
		done: function (r) {
			if( r.code == 0 ){
				layer.msg('上传成功!');
				let str = '',url = [];
				let arr = [], fileName = [];

				$("#brand").find(".closeBtn").each(function (k,v){
					arr.push({'url': $(v).find('a').attr('href'),name: $(v).find('p').text() })
				})
				arr.push({url:r.data.url,name:r.data.name});

				arr.forEach(function (v){
					url.push(v.url);
					fileName.push(v.name)
					str += `
						<div class="closeBtn">
							<a href="${v.url}" target="_blank">
								<img class="upImg" src="../../../img/pdf.png" width="95"/>
								<p>${v.name}</p>
							</a>
							<span class="iconclose"><img src="../../../img/close.png"></span>
						</div>
					  `;
				})

				$("#brand").html(str);
				$("#trainingPdf").val( url.join(',') );
				$("#fileName").val(fileName.join(','))

				$(".iconclose").click(function (){
					url = [];
					fileName = [];

					$(this).parent('.closeBtn').remove();
					$("#brand").find('.closeBtn').each(function (k,v){
						url.push($(v).find('a').attr('href'));
						fileName.push($(v).find('p').text())
					})
					$("#trainingPdf").val( url.join(',') );
					$("#fileName").val(fileName.join(','));
				})

			}
			else{
				layer.msg('上传失败!');
			}

		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});



layui.use('upload', function () {
	var upload = layui.upload;
	upload.render({
		elem: '#QRcodeAdd',
		url: '/common/sysFile/upload',
		accept: 'images', //只允许上传图片
		acceptMime: 'images/*', //只筛选图片
		exts:'jpg|png|gif|bmp|jpeg',
		size:10240,
		multiple:true,
		number:5,
		done: function (r) {
			if( r.code == 0 ){
				layer.msg('上传成功!');
				let str = '';

				str = `
					<div class="closeBtn">
						<img class="upImg" src="${r.data}" width="95"/>
						<span class="iconclose"><img src="../../../img/close.png"></span>
					</div>
				  `;

				$("#QRcodeImg").html(str);
				$("#qrCode").val( r.data );

				$(".iconclose").click(function (){
					$("#qrCode").val('');
					$("#QRcodeImg").html('');
				})

			}
			else{
				layer.msg('上传失败!');
			}

		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});


layui.use('upload', function () {
	var upload = layui.upload;
	upload.render({
		elem: '#columnPictureAdd',
		url: '/common/sysFile/upload',
		accept: 'images', //只允许上传图片
		acceptMime: 'images/*', //只筛选图片
		exts:'jpg|png|gif|bmp|jpeg',
		size:10240,
		multiple:true,
		number:5,
		done: function (r) {
			if( r.code == 0 ){
				layer.msg('上传成功!');
				let str = '';

				str = `
					<div class="closeBtn">
						<img class="upImg" src="${r.data}" width="95"/>
						<span class="iconclose"><img src="../../../img/close.png"></span>
					</div>
				  `;

				$("#columnPictureImg").html(str);
				$("#columnPicture").val( r.data );

				$(".iconclose").click(function (){
					$("#columnPicture").val('');
					$("#columnPictureImg").html('');
				})

			}
			else{
				layer.msg('上传失败!');
			}

		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});

$("#trainingForm").change(function (){

	if( $(this).val() == '1'){
		$("#address").hide()
	}else{
		$("#address").show()
	}
})